package com.aivin.lib_widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.appcompat.widget.AppCompatImageView;

/**
 * 有点击变暗效果
 */
public class MyColorFilterImageView extends AppCompatImageView {

    public MyColorFilterImageView(Context context) {
        super(context);
    }

    public MyColorFilterImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyColorFilterImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Drawable drawable = getDrawable();
        if(drawable==null){
            return super.onTouchEvent(event);
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                drawable.mutate().setColorFilter(Color.GRAY,  PorterDuff.Mode.MULTIPLY);
                break;

            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                drawable.mutate().clearColorFilter();
                //performClick();// 不要调用了，否则 在设置了 onclick的情况下 会回调2次
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }
}
